package com.hqyj.bigdata_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.bigdata_project.entity.Users;
import com.hqyj.bigdata_project.mapper.UsersMapper;
import com.hqyj.bigdata_project.service.UsersService;
import com.hqyj.bigdata_project.util.MyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.transaction.Transactional;


@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean register(Users u) throws Exception {
        try{
            return super.save(u);
        }catch (Exception ex){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            ex.printStackTrace();
            return false;
        }
        //return false;
    }

    @Override
    public Users login(String identify, String password) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        //判断identify是否是手机
        if(MyUtil.isMobileNumber(identify)){
            queryWrapper.eq("mobile",identify);
        }else if(MyUtil.isEmail(identify)){
            queryWrapper.eq("email",identify);
        }else{
            queryWrapper.eq("username",identify);
        }
        queryWrapper.eq("password",password);

        Users loginSuccessUser = super.getOne(queryWrapper);
        return loginSuccessUser;
    }
}
